<?php
namespace Modules\Frontend;

use Phalcon\DiInterface;

class Module implements \Phalcon\Mvc\ModuleDefinitionInterface {
  public function registerAutoloaders(DiInterface $di = null) {
    (new \Phalcon\Loader()) -> registerNamespaces([
      'Components' => APPLICATION_SRC_DIR . '/components',

      __NAMESPACE__ . '\\Controllers' => __DIR__ . '/controllers/',
    ]) -> register();
  }

  public function registerServices(DiInterface $di) {
    $di -> setShared('dispatcher', function() {
      $dispatcher = new \Phalcon\Mvc\Dispatcher();
      $dispatcher -> setDefaultNamespace(__NAMESPACE__ . '\\Controllers');
      return $dispatcher;
    });

    $di -> setShared('view', function () {
      return (new \Phalcon\Mvc\View()) -> disable();
    });

    $di -> setShared('config', function() {
      return new \Phalcon\Config(require(APPLICATION_SRC_DIR . '/config/' . APPLICATION_ENV . '.php'));
    });

    $di -> setShared('db', function () {
      $db = $this -> getShared('config') -> get('DATABASE_MASTER') -> get(0);

      $class = 'Phalcon\\Db\\Adapter\\Pdo\\' . $db -> adapter;
      return new $class([
        'host'     => $db -> host,
        'username' => $db -> username,
        'password' => $db -> password,
        'port'     => $db -> port,
        'dbname'   => $db -> dbname,
        'charset'  => $db -> charset
      ]);
    });

    $di -> setShared('modelsMetadata', function() {
      /*
      if(APPLICATION_ENV === 'development') {
        return new \Phalcon\Mvc\Model\MetaData\Memory();
      }
      */

      $metaDataDir = $this -> getShared('config') -> get('RUNTIME_METADATA_DIR') . '/frontend/';
      //if(!is_dir($metaDataDir)) { mkdir($metaDataDir, 0777, true); }
      return new \Phalcon\Mvc\Model\MetaData\Files([
        'metaDataDir' => $metaDataDir
      ]);
    });

    $di -> setShared('cacheBackend', function() {
      $cacheDir = $this -> getShared('config') -> get('RUNTIME_CACHE_DIR') . '/frontend/';
      //if(!is_dir($cacheDir)) { mkdir($cacheDir, 0777, true); }

      return new \Phalcon\Cache\Backend\File(
        new \Phalcon\Cache\Frontend\Data([
          'lifetime' => 172800
        ]),
        [
          'cacheDir' => $cacheDir
        ]
      );
    });

    $di -> set('session', function() {
      $session = new \Phalcon\Session\Adapter\Files();
      $session -> start();
      return $session;
    });
/*
    $di->setShared('security', function () {
      return (new \Phalcon\Security()) -> setWorkFactor(12);
    });
*/

    $di -> setShared('crypt', function () {
      $key = $this -> getShared('cacheBackend') -> get('crypt_key');
      if(!$key) {
        $key = $this -> getShared('security') -> getRandom() -> base64Safe(8);
        $this -> getShared('cacheBackend') -> save('crypt_key', $key);
      }

      return (new \Phalcon\Crypt()) -> setKey($key);
    });

    $di -> setShared('user', function() {
      return \Components\User\Adapter::load([
        'adapter' => 'Sha',
        'Sha' => []
      ]);
    });

    $di -> setShared('parameters', function() {
      return \Components\Parameters\Adapter::load([
        'adapter' => 'Sha',
        'Sha' => []
      ]);
    });
  }
}
